package com.sunday.factory.parser;

import com.sunday.factory.config.RuleConfig;
import com.sunday.factory.factory.RuleConfigParserFactory;
import com.sunday.factory.parser.type.IRuleConfigParser;
import com.sunday.factory.parser.type.impl.JsonRuleConfigParser;
import com.sunday.factory.parser.type.impl.XmlRuleConfigParser;

/**
 * @ClassName RuleConfigSource
 * @Author JiangHai
 * @Date 2021/9/5 19:01
 **/
public class RuleConfigSource {

    public RuleConfig load(String ruleConfigFilePath) {
        String ruleConfigFileExtension = getFileExtension(ruleConfigFilePath);
        IRuleConfigParser parser = RuleConfigParserFactory.createParser(ruleConfigFileExtension);
        if (parser == null) {
            throw new IllegalArgumentException();
        }
        String configText = "";
        //从ruleConfigFilePath文件中读取配置文本到configText中
        return parser.parser(configText);
    }

    private String getFileExtension(String filePath) {
        //...解析文件名获取扩展名，比如rule.json，返回json
        if (filePath == null || filePath.isEmpty()) {
            throw new IllegalArgumentException("file path is empty");
        }
        return filePath.split(".")[1];
    }
}
